package org.smart4j.plugin.security;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.mgt.CachingSecurityManager;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.smart4j.plugin.security.realm.SmartCustomRealm;
import org.smart4j.plugin.security.realm.SmartJdbcRealm;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author djw E-mail:***@***.com
 * @version 创建时间：2017/10/29 21:05
 */
public class SmartSecurityFilter extends ShiroFilter {
    @Override
    public void init() throws Exception {
        super.init();
        WebSecurityManager webSecurityManager=super.getSecurityManager();
        setRealms(webSecurityManager);
        setCache(webSecurityManager);
    }

    private void setCache(WebSecurityManager webSecurityManager) {
        if(SecurityConfig.isCache()){
            CachingSecurityManager cachingSecurityManager= (CachingSecurityManager) webSecurityManager;
            CacheManager cacheManager=new MemoryConstrainedCacheManager();
            cachingSecurityManager.setCacheManager(cacheManager);
        }
    }

    private void setRealms(WebSecurityManager webSecurityManager) {
        String securityRealms=SecurityConfig.getRealms();
        if(securityRealms!=null){
            String[] securityRealmArray=securityRealms.split(",");
            if(securityRealmArray.length>0){
                Set<Realm> realms=new LinkedHashSet<>();
                for(String realm:securityRealmArray){
                    if(realm.equalsIgnoreCase(SecurityConstant.REALMS_JDBC)){
                        addJdbcRealm(realms);
                    }else if(realm.equalsIgnoreCase(SecurityConstant.REALMS_CUSTOM)) {
                                addCustomRealm(realms);
                    }
                }
                RealmSecurityManager realmSecurityManager= (RealmSecurityManager) webSecurityManager;
                realmSecurityManager.setRealms(realms);
            }
        }
    }

    private void addCustomRealm(Set<Realm> realms) {
        SmartSecurity smartSecurity=SecurityConfig.getSmartSecurity();
        SmartCustomRealm smartCustomRealm=new SmartCustomRealm(smartSecurity);
        realms.add(smartCustomRealm);
    }

    private void addJdbcRealm(Set<Realm> realms) {
        SmartJdbcRealm smartJdbcRealm=new SmartJdbcRealm();
        realms.add(smartJdbcRealm);
    }

}
